Test Failed
Push — es2015 ( b5c188...64e5b0 )
by Luís
01:49
created

index.js ➔ ... ➔ ???   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 2
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
1
export default {
2
    /**
3
     * Sort an array of objects by one prop of objects
4
     * @param {Array} data
5
     * @param {String} prop
6
     * @param {String} direction defines if sort should be asc or desc
7
     */
8
    sortByObjectKey: function (data, prop, direction = "asc") {
9
        if (["asc", "desc"].indexOf(direction) === -1) {
10
            throw new Error("Direction should be asc or desc");
11
        }
12
13
        return data.sort((a, b) => {
14
            let response = 0,
15
                ap = a[prop],
16
                bp = b[prop];
17
18
            if (ap < bp) {
19
                response = direction === "asc" ? -1 : 1;
20
            } else if (ap > bp) {
21
                response = direction === "asc" ? 1 : -1;
22
            }
23
24
            return response;
25
        });
26
    },
27
28
    /**
29
     * Filter objects from array that has key value in values
30
     * Example:
31
     * var arr = [{a: 1}, {a: 2}, {a: 3}]
32
     * App.helpers.array.filterBy("a", [1, 3], arr);
33
     *
34
     * The output will be:
35
     * [{a: 1}, {a: 3}]
36
     * @param key
37
     * @param values
38
     * @param items
39
     */
40
    filterBy: function (key, values, items) {
41
        return items.filter(item => values.indexOf(item[key]) > -1);
42
    }
43
}
44